package com.suoluo.zenadmin.controllers.mobile;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;

import com.suoluo.zenadmin.common.UBB;
import com.suoluo.zenadmin.common.config.SystemConfigHolder;
import com.suoluo.zenadmin.service.dest.DestService;
import com.suoluo.zenadmin.service.dest.MobileService;
import com.suoluo.zenadmin.vo.country.ContinentVO;
import com.suoluo.zenadmin.vo.dest.DestFeature2VO;
import com.suoluo.zenadmin.vo.dest.DestFeatureItem2VO;
import com.suoluo.zenadmin.vo.dest.DestVO;

/**
 * 目的地访问的action
 * 
 * @author xiongxu
 * 
 */
@Path("")
public class MobileDestController {

	@Resource(name = "destService")
	private DestService destService;

	@Resource(name = "mobileService")
	private MobileService mobileService;

	/**
	 * http://www.zentravel.cn/mobile/dest.do?action=viewDestHome
	 * 
	 * @return
	 */
	@Get("dest.do")
	public String get(Invocation inv, @Param("action") String action,
			@Param("destId") int destId) {
		if ("viewDestHome".equals(action)) {
			return viewDestHome(inv);
		} else if ("viewDest".equals(action)) {
			return viewDest(inv, destId);
		} else if ("viewFeature".equals(action)) {
			return viewFeature(inv, destId);
		} else if ("viewFeatureItem".equals(action)) {
			return viewFeatureItem(inv);
		} else if ("navFeatureItem".equals(action)) {
			return navFeatureItem(inv);
		} else {
			return "@Not Found";
		}
	}

	/**
	 * 在目的地特色类别详细页点击推荐的特色项，调此方法跳转到目的地特色项详细页
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	private String viewFeatureItem(Invocation inv) {
		int featureItemId = Integer.parseInt(inv.getParameter("featureItemId"));
		featureItemInfo(inv, featureItemId);

		return "dest/featureItem";

	}

	/**
	 * 根据特色项Id获取特色项信息
	 * 
	 * @param featureItemId
	 * @return
	 */
	private void featureItemInfo(Invocation inv, int featureItemId) {
		DestFeatureItem2VO featureItem = destService
				.findFeatureItemById(featureItemId);
		featureItem.setContent(UBB.decode(featureItem.getContent()));
		inv.addModel("featureItem", featureItem);

		DestFeature2VO destFeature = destService.findSimplestDestFeatureById(featureItem.getFeatureId());
		inv.addModel("feature", destFeature);
		// 获取目的地名称
//		TravelDestinationPOJO dest = destService
//				.findTravelDestinationById(destFeature.getFeatureDestId());
		DestVO dest = destService.findSimplestDestById(destFeature.getDestId());
		inv.addModel("dest", dest);
		// 查询特色项所在的类别
//		DestFeatureCategoryPOJO featureCategory = destService
//				.findDestFeatureCategoryById(destFeature.getFeatureCategoryId());
//		
		// mv.addObject("categorySpecial",
		// SystemConfigHolder.getDestFeatureCategorySpecial());
		int pageSize = 4;

		List<DestFeatureItem2VO> samilarFeatureItems = destService
				.findSamilarDestFeatureItems(featureItemId,
						featureItem.getFeatureId(), pageSize);
		inv.addModel("samilarFeatureItems", samilarFeatureItems);
	}

	/**
	 * 在目的地详细页点击推荐的目的地下特色类别，调此方法跳转到该类别的详细页
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	private String viewFeature(Invocation inv, int destId) {
		Integer featureId = Integer.valueOf(inv.getParameter("featureId"));

		// 1.目的地详细信息
//		TravelDestinationPOJO dest = mobileService.findTravelDestinationByFeatureId(featureId);
//		dest.setDestDesc(UBB.decode(dest.getDestDesc()));
		DestVO dest = mobileService.findSimplestDestByFeatureId(featureId);
		inv.addModel("dest", dest);
		// 获取选中特色的详细信息
		DestFeature2VO feature = destService.findDestFeatureById(featureId);
		inv.addModel("feature", feature);
		feature.setContent(UBB.decode(feature.getContent()));
		// 根据特色分类Id获取分类名称
//		Integer featureCategoryId = feature.getFeatureCategoryId();
//		DestFeatureCategoryPOJO featureCategory = destService
//				.findDestFeatureCategoryById(featureCategoryId);
//		inv.addModel("featureCategory", featureCategory);
		// int contentLength = SystemConfigHolder.getMobileContentLimit();
		List<DestFeatureItem2VO> featureItems = destService
		.findFeatureItemsWithoutContentByFeatureId(featureId);
		if (feature.getFeatureCategoryId() == SystemConfigHolder.getDestFeatureCategoryForHotel()) {
//			List<HotelPOJO> hotels = destService
//					.findHotelFeatureItems(featureId);
			inv.addModel("hotels", featureItems);
			return "dest/featureForHotel";
		} else {
			// 获取选中特色的特色项
			inv.addModel("featureItems", featureItems);
			return "dest/feature";
		}
	}

	/**
	 * 查询目的地详细信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	private String viewDest(Invocation inv, int destId) {
		// 1.目的地详细信息
		DestVO dest = destService.findDestById(destId);
//		TravelDestinationPOJO dest = destService
//				.findTravelDestinationById(destId);
		dest.setContent(UBB.decode(dest.getContent()));
		inv.addModel("dest", dest);
		// 2.特别体验类别配置
		int categoryId = SystemConfigHolder.getDestFeatureCategorySpecial();

		// 3.查询特色信息
		List<DestFeature2VO> featureList = mobileService
				.findDestFeatures(destId);
		for (Iterator<DestFeature2VO> iterator = featureList.iterator(); iterator.hasNext();) {
			DestFeature2VO destFeature = iterator.next();
			if (destFeature.getFeatureCategoryId() == categoryId) {
				inv.addModel("experience", destFeature);
				iterator.remove();
				break;
			}
		}

		inv.addModel("features", featureList);

		return "dest/dest";
	}

	/**
	 * 查询目的地主页的接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	private String viewDestHome(Invocation inv) {
		// 1.洲信息集合
		List<ContinentVO> coninentTree = destService.findDestNavigateTree();
		inv.addModel("coninentTree", coninentTree);
		return "dest/destOverview";
	}

	/**
	 * 从导航页点击某一特色进入特色项列表页面，点击某一具体的特色项调此方法
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	private String navFeatureItem(Invocation inv) {
		int featureItemId = Integer.parseInt(inv.getParameter("featureItemId"));
		featureItemInfo(inv, featureItemId);

		return "dest/navFeatureItem";

	}

}
